﻿using System;
using System.Collections.Generic;
using Tools.Domain.Specifications;
using Tools.Domain.Specifications.Constraints;

namespace Webshop20.Domain.Customers.Core
{
    [Serializable]
    public class Adress
    {
        public virtual string Street { get; private set; }
        public virtual string City { get; private set; }
        public virtual string PostalCode { get; private set; }
        public virtual string Country { get; private set; }

        public Adress(){}

        public Adress(string street,
                        string city,
                        string postalCode,
                        string country
                     )
        {
            Street = street;
            City = city;
            PostalCode = postalCode;
            Country = country;
          
        }

        private bool Validate(out IEnumerable<ISpecificationError> specificationErrors, IConstraintSet<Adress> adressConstraintSet)
        {
            bool validationResult = adressConstraintSet.InvarantsAreSatisfiedBy(this);
            specificationErrors = adressConstraintSet.GetInvariantsSpecificationErrors();
            return validationResult;
            
        }

        public static Adress Create(string street, string city, string postalCode, string country, out IEnumerable<ISpecificationError> specificationErrors)
        {
            var newAdress = new Adress(street, city, postalCode, country);
            var validationResult = newAdress.Validate(out specificationErrors, new AdressConstraintSet(new ExecuteAlways()));

            if (!validationResult)
                return null;
            
            return newAdress;
            
        }
    }
}